package com.zeny.edu.video.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.zeny.edu.common.CookieUtil;
import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.video.client.UcenterClient;
import com.zeny.edu.video.service.VidService;
import com.zeny.edu.video.util.AliyunVODSDKUtils;
import com.zeny.edu.video.util.ConstantPropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName VideoController
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/6 0006 23:52
 */
@RestController
@RequestMapping("/videoservice/vod")
@CrossOrigin
public class VideoController {

    @Autowired
    private VidService vidService;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * @Description 根据视频ID获取视频播放路径
     * @Date 2020/3/10 0010 23:03
     * @param vid
     * @return com.zeny.edu.common.ResponseResult
     **/
    @GetMapping("/getPlayUrl/{vid}")
    public ResponseResult getPlayUrlById(@PathVariable String vid, HttpServletRequest requests) throws Exception{
        String token = CookieUtil.getCookieValue(requests, "token", true);
        if (StringUtils.isBlank(token) || !ucenterClient.getUserInfo(token).getSuccess()) {
            return ResponseResult.fail().data("playUrl",null);
        }
        String playUrl = "";
        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        DefaultAcsClient client = AliyunVODSDKUtils.initVodClient(accessKeyId, accessKeySecret);
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(vid);
        response = client.getAcsResponse(request);
        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        //播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.println("PlayInfo.PlayURL = " + playInfo.getPlayURL());
            if (StringUtils.isNotBlank(playInfo.getPlayURL()) && playInfo.getPlayURL().contains(".mp4")) {
                playUrl = playInfo.getPlayURL();
            }
        }
        //Base信息
        System.out.println("VideoBase.Title = " + response.getVideoBase().getTitle());
        System.out.println("RequestId = " + response.getRequestId());
        return ResponseResult.ok().data("playUrl", playUrl);
    }


    /**
     * @param vid
     * @return com.zeny.edu.common.ResponseResult
     * @Description 根据videoId获取视频播放凭证
     * @Date 2020/3/10 0010 22:39
     **/
    @GetMapping("/getPlayAuth/{vid}")
    public ResponseResult getPlayAuthById(@PathVariable String vid) throws Exception {
        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        //初始化
        DefaultAcsClient client = AliyunVODSDKUtils.initVodClient(accessKeyId, accessKeySecret);
        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(vid);
        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        //得到播放凭证
        String playAuth = response.getPlayAuth();
        return ResponseResult.ok().data("playAuth", playAuth);
    }

    /**
     * @param file
     * @return com.zeny.edu.common.ResponseResult
     * @Description 视频上传到阿里云
     * @Date 2020/3/6 0006 23:59
     **/
    @PostMapping("/upload")
    public ResponseResult uploadAliyunVideo(@RequestParam("file") MultipartFile file) {
        String videoId = vidService.uploadVideoAliyun(file);
        return ResponseResult.ok().data("videoId", videoId);
    }

    @DeleteMapping("/{videoId}")
    public ResponseResult removceVideo(@PathVariable String videoId) {
        vidService.removeVideo(videoId);
        return ResponseResult.ok();
    }

    /**
     * @param videoList
     * @return com.zeny.edu.common.ResponseResult
     * @Description 删除多个视频
     * @Date 2020/3/8 0008 8:06
     **/
    @DeleteMapping("/removeMoreVideo")
    public ResponseResult deleteMoreVideo(@RequestParam("videoList") List<String> videoList) {
        vidService.deleteMoreVideo(videoList);
        return ResponseResult.ok();
    }


}
